<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Communitybaseinfo extends Model
{
    // 表名
    protected $name = 'communitybaseinfo';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    
    // 追加属性
    protected $append = [
        'status_text',
        'school_text',
        'location_text',
        'admin_text',
        'subject_lv_text'
    ];

    // 状态列表
    public function getStatusList()
    {
        return ['0' => '失效', '1' => '正常'];
    }

    // 获取状态文本
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function status()
    {
        return 1;
    }
    
    // 获取学校名称
    public function getSchoolTextAttr($value, $data)
    {
        $school_id = isset($data['school_id']) ? $data['school_id'] : '';
        if ($school_id) {
            try {
                $school = Db::name('eb_school')->where('id', $school_id)->find();
                return $school ? $school['name'] : '';
            } catch (\Exception $e) {
                return '';
            }
        }
        return '';
    }
    
    // 获取教学点名称
    public function getLocationTextAttr($value, $data)
    {
        $ocation_id = isset($data['ocation_id']) ? $data['ocation_id'] : '';
        if ($ocation_id) {
            try {
                $location = Db::name('fa_ocation')->where('id', $ocation_id)->find();
                return $location ? $location['name'] : '';
            } catch (\Exception $e) {
                return '';
            }
        }
        return '';
    }
    
    // 获取管理员名称
    public function getAdminTextAttr($value, $data)
    {
        $admin_id = isset($data['admin_id']) ? $data['admin_id'] : '';
        if ($admin_id) {
            try {
                $admin = Db::name('fa_admin')->where('id', $admin_id)->find();
                return $admin ? $admin['username'] : '';
            } catch (\Exception $e) {
                return '';
            }
        }
        return '';
    }
    
    // 获取学科等级名称
    public function getSubjectLvTextAttr($value, $data)
    {
        $subject_lv = isset($data['subject_lv']) ? $data['subject_lv'] : '';
        if ($subject_lv) {
            try {
                $subject = Db::name('fa_subject_lv')->where('id', $subject_lv)->find();
                return $subject ? $subject['name'] : '';
            } catch (\Exception $e) {
                return '';
            }
        }
        return '';
    }

}